package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import Other.Glob;
import database.DatabaseExtension;
import database.MyAddressee;
import database.MyCountry;
import database.MyTax;
import database.NewTirDataBase;
import enums.DialogType;
import enums.SearchType;

public class Addressee extends JFrame {

	//variable for singleton
		private static Addressee addresseeInstance = null;
		private JPanel contentPane;
		private JTextField tfCode;
		private JTextField tfName;
		//variable that represent this window frame
		private JFrame thisFrame = this;
		//variable to enable and disable the screen
		boolean disable;
		private JTextField tfAddress;
		private JTextField tfPostcode;
		private JTextField tfTown;
		private JTextField tfProvince;
		private JTextField tfEmail;
		private JTextField tfContact;
		private JTextField tfNif;
		private JTextField tfFax;
		private JTextField tfPhone;
		private JTextField tfCountryCode;
		private JTextField tfCountryName;
		public Addressee() {
			//set the title
			setTitle("Addressee");
			setVisible(true);
			//set resizable
			setResizable(false);
			setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
			setBounds(100, 100, 864, 432);
			contentPane = new JPanel();
			contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
			setContentPane(contentPane);
			JPanel leftPanel = new JPanel();
			leftPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			JPanel buttonPanel = new JPanel();
			buttonPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			
			JPanel rightPanel = new JPanel();
			rightPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
			GroupLayout gl_contentPane = new GroupLayout(contentPane);
			gl_contentPane.setHorizontalGroup(
				gl_contentPane.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_contentPane.createSequentialGroup()
						.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_contentPane.createSequentialGroup()
								.addComponent(leftPanel, GroupLayout.PREFERRED_SIZE, 412, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(rightPanel, GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE))
							.addComponent(buttonPanel, GroupLayout.DEFAULT_SIZE, 790, Short.MAX_VALUE))
						.addContainerGap())
			);
			gl_contentPane.setVerticalGroup(
				gl_contentPane.createParallelGroup(Alignment.TRAILING)
					.addGroup(gl_contentPane.createSequentialGroup()
						.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
							.addComponent(rightPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(leftPanel, GroupLayout.PREFERRED_SIZE, 302, GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addGap(5))
			);
			
			JLabel lblEmail = new JLabel("Email");
			
			tfEmail = new JTextField();
			tfEmail.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfCode.requestFocus();				
					}
				}
			});
			tfEmail.setColumns(10);
			
			tfContact = new JTextField();
			tfContact.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfEmail.requestFocus();				
					}
				}
			});
			tfContact.setColumns(10);
			
			JLabel lblContact = new JLabel("Contact");
			
			JLabel lblNifcif = new JLabel("Nif,Cif");
			
			tfNif = new JTextField();
			tfNif.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfContact.requestFocus();				
					}
				}
			});
			tfNif.setColumns(10);
			
			tfFax = new JTextField();
			tfFax.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfNif.requestFocus();				
					}
				}
			});
			tfFax.setColumns(10);
			
			JLabel lblFax = new JLabel("Fax");
			
			JLabel label_4 = new JLabel("Phone");
			
			tfPhone = new JTextField();
			tfPhone.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfFax.requestFocus();				
					}
				}
			});
			tfPhone.setColumns(10);
			
			tfCountryCode = new JTextField();
			tfCountryCode.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						//enterDataCountry();
						ScreenHandling.enterData(tfCountryCode, tfCountryName, tfPhone, SearchType.COUNTRY);
						System.out.println("button is being pressed");
						
					}
				}
			});
			tfCountryCode.setColumns(10);
			
			JLabel lblCountry = new JLabel("Country");
			
			JButton searchButtonCountry = new JButton("F5 Search");
			
			searchButtonCountry.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					 Glob.type = SearchType.ADDRESSEECOUNTRY;
					 //when textcode focused and f5 pressed search dialog comes up
					search();
				}
			});
			
			tfCountryName = new JTextField();
			tfCountryName.setEditable(false);
			tfCountryName.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfPhone.requestFocus();				
					}
				}
			});
			tfCountryName.setColumns(10);
			
			GroupLayout gl_rightPanel = new GroupLayout(rightPanel);
			gl_rightPanel.setHorizontalGroup(
				gl_rightPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_rightPanel.createSequentialGroup()
						.addGap(41)
						.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_rightPanel.createSequentialGroup()
								.addComponent(lblCountry, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
								.addGap(10)
								.addComponent(tfCountryCode, GroupLayout.PREFERRED_SIZE, 45, GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(tfCountryName, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)
								.addGap(18)
								.addComponent(searchButtonCountry))
							.addGroup(gl_rightPanel.createSequentialGroup()
								.addComponent(lblEmail, GroupLayout.PREFERRED_SIZE, 41, GroupLayout.PREFERRED_SIZE)
								.addGap(37)
								.addComponent(tfEmail, 241, 241, 241))
							.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING, false)
								.addGroup(gl_rightPanel.createSequentialGroup()
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(lblContact, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(lblNifcif, GroupLayout.DEFAULT_SIZE, 44, Short.MAX_VALUE))
									.addGap(34)
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.TRAILING)
										.addComponent(tfNif, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)
										.addComponent(tfContact, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)))
								.addGroup(gl_rightPanel.createSequentialGroup()
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.TRAILING, false)
										.addComponent(label_4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
										.addComponent(lblFax, GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
									.addGap(39)
									.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
										.addComponent(tfFax)
										.addComponent(tfPhone, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE)))))
						.addGap(29))
			);
			gl_rightPanel.setVerticalGroup(
				gl_rightPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_rightPanel.createSequentialGroup()
						.addGap(39)
						.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_rightPanel.createSequentialGroup()
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(4)
										.addComponent(lblCountry))
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(1)
										.addGroup(gl_rightPanel.createParallelGroup(Alignment.BASELINE)
											.addComponent(tfCountryCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
											.addComponent(tfCountryName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
								.addGap(20)
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(3)
										.addComponent(label_4))
									.addComponent(tfPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(21)
										.addComponent(lblFax))
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(18)
										.addComponent(tfFax, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
								.addGap(18)
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(3)
										.addComponent(lblNifcif))
									.addComponent(tfNif, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
								.addGap(18)
								.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
									.addGroup(gl_rightPanel.createSequentialGroup()
										.addGap(3)
										.addComponent(lblContact))
									.addComponent(tfContact, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addComponent(searchButtonCountry))
						.addGroup(gl_rightPanel.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_rightPanel.createSequentialGroup()
								.addGap(21)
								.addComponent(lblEmail))
							.addGroup(gl_rightPanel.createSequentialGroup()
								.addGap(18)
								.addComponent(tfEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGap(48))
			);
			rightPanel.setLayout(gl_rightPanel);
			JButton btnSave = new JButton("F4 Save");
			btnSave.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					//Save when button pressed
					Glob.type = SearchType.ADDRESSEE;
					try {
						saveDialog();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
			tfCode = new JTextField();
			tfCode.getInputMap(tfCode.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), "Save");
			tfCode.getActionMap().put("Save", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					//Save when button pressed
					 Glob.type = SearchType.ADDRESSEE;
					 try {
						saveDialog();
					} catch (Exception e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				 }
			});
			JButton btnRemove = new JButton("F6 Remove");
			//set up F6 for removing
			btnRemove.getInputMap(btnRemove.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F6, 0), "");
			btnRemove.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 //remove item when F6 button pressed
					 ScreenHandling.remove(tfCode, SearchType.ADDRESSEE);
				    }
			});
			btnRemove.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//remove item when button pressed
					 ScreenHandling.remove(tfCode, SearchType.ADDRESSEE);
				}
			});
			JButton btnFReset = new JButton("F7 Reset");
			//set up F7 for reset
			btnFReset.getInputMap(btnFReset.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0), "");
			btnFReset.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					//reset field when button pressed
					 resetFields();
				    }
			});
			btnFReset.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//reset field when button pressed
					resetFields();
				}
			});
			JButton btnEscExit = new JButton("Esc Exit");
			//set up Escape for exit
			btnEscExit.getInputMap(btnEscExit.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
			btnEscExit.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					if (disable == false){
					//escape from the screen
						escape();
					}
				    }
			});
			btnEscExit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//if the screen is disable it is possible to escape
					if (disable == false){
						escape();
					}
				}
			});
			GroupLayout gl_buttonPanel = new GroupLayout(buttonPanel);
			gl_buttonPanel.setHorizontalGroup(
				gl_buttonPanel.createParallelGroup(Alignment.TRAILING)
					.addGroup(gl_buttonPanel.createSequentialGroup()
						.addGap(18)
						.addComponent(btnRemove)
						.addPreferredGap(ComponentPlacement.RELATED, 220, Short.MAX_VALUE)
						.addComponent(btnEscExit)
						.addGap(29)
						.addComponent(btnFReset)
						.addGap(34)
						.addComponent(btnSave)
						.addGap(18))
			);
			gl_buttonPanel.setVerticalGroup(
				gl_buttonPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_buttonPanel.createSequentialGroup()
						.addGap(21)
						.addGroup(gl_buttonPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(btnSave)
							.addComponent(btnRemove)
							.addComponent(btnFReset)
							.addComponent(btnEscExit))
						.addContainerGap(38, Short.MAX_VALUE))
			);
			buttonPanel.setLayout(gl_buttonPanel);
			JLabel lblCode = new JLabel("Code");
			JLabel lblName = new JLabel("Name");
			//set up F5 for saving only when the text is focused
			tfCode.getInputMap(tfCode.WHEN_FOCUSED)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
			tfCode.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 Glob.type = SearchType.ADDRESSEE;
					 //when textcode focused and f5 pressed search dialog comes up
					search();
				    }
			});
			JButton searchButtonCode = new JButton("F5 Search");
			searchButtonCode.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					 Glob.type = SearchType.ADDRESSEE;
					 //when textcode focused and f5 pressed search dialog comes up
					search();
				}
			});
			tfCode.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a number is entered with ENTER
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						
						enterData();
						
						//tfName.requestFocus();
					}
					
				}
			});
			tfCode.setColumns(10);
			tfName = new JTextField();
			tfName.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a number is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfAddress.requestFocus();	
					}
				}
			});
			tfName.setColumns(10);
			
			JLabel lblAddress = new JLabel("Address");
			
			tfAddress = new JTextField();
			tfAddress.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfPostcode.requestFocus();				
					}
				}
			});
			
			tfAddress.setColumns(10);
			
			JLabel lblPostcode = new JLabel("Postcode");
			
			tfPostcode = new JTextField();
			tfPostcode.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfTown.requestFocus();				
					}
				}
			});
			tfPostcode.setColumns(10);
			
			JLabel lblTown = new JLabel("Town");
			
			tfTown = new JTextField();
			tfTown.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfProvince.requestFocus();				
					}
				}
			});
			tfTown.setColumns(10);
			
			JLabel lblProvince = new JLabel("Province");
			
			tfProvince = new JTextField();
			tfProvince.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a type is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						//request focus for the next fields
						tfCountryCode.requestFocus();				
					}
				}
			});
			//set up F5 for searching only when the text is focused
			tfCountryCode.getInputMap(tfCode.WHEN_FOCUSED)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F5, 0), "");
			tfCountryCode.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 
					 //when textcode focused and f5 pressed search dialog comes up
					 Glob.type= SearchType.ADDRESSEECOUNTRY;
					 
					search();
					
				    }
			});
			tfProvince.setColumns(10);
			GroupLayout gl_leftPanel = new GroupLayout(leftPanel);
			gl_leftPanel.setHorizontalGroup(
				gl_leftPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_leftPanel.createSequentialGroup()
						.addGap(42)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING)
							.addComponent(lblCode, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
							.addComponent(lblName)
							.addComponent(lblAddress)
							.addComponent(lblPostcode)
							.addComponent(lblTown)
							.addComponent(lblProvince))
						.addPreferredGap(ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(tfName)
							.addGroup(Alignment.LEADING, gl_leftPanel.createSequentialGroup()
								.addGroup(gl_leftPanel.createParallelGroup(Alignment.LEADING, false)
									.addComponent(tfCode, GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
									.addComponent(tfPostcode)
									.addComponent(tfTown)
									.addComponent(tfProvince))
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(searchButtonCode, GroupLayout.PREFERRED_SIZE, 107, GroupLayout.PREFERRED_SIZE))
							.addComponent(tfAddress, Alignment.LEADING))
						.addGap(47))
			);
			gl_leftPanel.setVerticalGroup(
				gl_leftPanel.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_leftPanel.createSequentialGroup()
						.addGap(37)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblCode)
							.addComponent(tfCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(searchButtonCode))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblName)
							.addComponent(tfName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblAddress)
							.addComponent(tfAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblPostcode)
							.addComponent(tfPostcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblTown)
							.addComponent(tfTown, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(gl_leftPanel.createParallelGroup(Alignment.BASELINE)
							.addComponent(lblProvince)
							.addComponent(tfProvince, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGap(134))
			);
			//request focus when object is called
			tfCode.requestFocus(true);
			leftPanel.setLayout(gl_leftPanel);
			contentPane.setLayout(gl_contentPane);	
		}
	//singleton
	public static Addressee getInstance(){
		if(addresseeInstance == null){
			addresseeInstance = new Addressee();
		}
		return addresseeInstance;
		}
	//initiate data
	public void init(MyAddressee addressee){
		
	
		
		tfCode.setText(addressee.getAddresseeID());	
		tfName.setText(addressee.getName());	
		tfAddress.setText(addressee.getAddress());
		tfPostcode.setText(addressee.getPostcode());
		tfTown.setText(addressee.getTown());
		tfProvince.setText(addressee.getProvince());
		if(String.valueOf(addressee.getCodeCountryID()).equals("null")){
			
			tfCountryCode.setText("");
			
		}else{
		tfCountryCode.setText(addressee.getCodeCountryID());
		}
		tfCountryName.setText(addressee.getCountryName());
		tfPhone.setText(addressee.getPhoneNumber());
		tfFax.setText(addressee.getFax());
		tfNif.setText(addressee.getNif());
		tfContact.setText(addressee.getContact());
		tfEmail.setText(addressee.getEmail());
		
	}
	
	public void initCountry(MyCountry country){
		
		tfCountryCode.setText(country.getCountryID());
		tfCountryName.setText(country.getCountryName());
		
	}
	
	//method needed to set focus from other screens
	public void setTextCodeFocus(){
		tfCode.requestFocus();
	}
	//reset fields
	public void resetFields(){
		tfCode.setText("");
		tfName.setText("");
		tfAddress.setText("");
		tfPostcode.setText("");
		tfTown.setText("");
		tfProvince.setText("");
		tfCountryCode.setText("");
		tfCountryName.setText("");
		tfPhone.setText("");
		tfFax.setText("");
		tfNif.setText("");
		tfContact.setText("");
		tfEmail.setText("");
		tfCode.requestFocus();
	}
	//method needed to get value from text code in other screens
	public String getFieldCode() {
	    return tfCode.getText();
	 }
	//method needed to get value from text Country field in other screens
	public String getFieldCountry() {
		return tfCountryCode.getText();
	}
	//method for saving the object
	public void saveDialog() throws Exception{
		//Glob.type = SearchType.TAX;
		Glob.dialog=DialogType.OVERWRITE;
		try{
		if(tfName.getText().trim().length()<1) {
			JOptionPane.showMessageDialog(null, "Name may not be empty");
			tfName.requestFocus();
			return;
		}
		if(!tfCode.getText().trim().isEmpty()) {
			
			if(Integer.parseInt(tfCode.getText())<1){
				JOptionPane.showMessageDialog(null, "Code has to be a valid number");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
				
			}
			}
		
		ScreenHandling.checkIdOnTable(tfCountryCode, SearchType.COUNTRY);
	
		ArrayList<Integer> ids = DatabaseExtension.columnIDs("Select * from Addressee");
		//System.out.println(ids);
		boolean Ok = false;
		if(tfCode.getText().trim().length()<1) {
			//Do nothing
			}else{
				
		for (int i: ids) {
			if (i == Integer.parseInt(tfCode.getText()))
					Ok=true;
		}		
			if (Ok == true) {
			//JOptionPane.showMessageDialog(null, "This ID is not on the Tax table");
			Dialog d = Dialog.getInstance();
			d.setVisible(true);
			Addressee.getInstance().disableAddressee();
			//tfCode.requestFocus();
			return;
			
		}
			}
			if(Ok==false){
			save();			
			}
			
	}catch (NumberFormatException e){
		System.out.println("This is the general expetion inside saveDialog");
		JOptionPane.showMessageDialog(null, "Code must be a positive number");
		tfCode.setText("");
		tfCode.requestFocus();
		e.printStackTrace();
		return;
		//save();	

	 
	}
	}
	//method that saves the object in the database
	public void save(){
		
		Glob.type = SearchType.ADDRESSEE;
		
		String[] data = new String[12];	
		try{
		data[0] = tfCode.getText();	
		}catch(Exception code){
			
			code.printStackTrace();
			
		}
		System.out.println("This is the id got from the tfcode inside save" + data[0]);
		if(data[0].isEmpty()){
		data[0] = null;
		}
	data[1] = tfName.getText();
	
	data[2] = tfAddress.getText();
	
	data[3] = tfPostcode.getText();
	
	data[4] = tfTown.getText();
	
	data[5] = tfProvince.getText();
	
	System.out.println("This is the length of the country code in the save method "+ tfCountryCode.getText().trim().length());
	
	try{
		if(tfCountryCode.getText().isEmpty()){
			
			data[6] = null;
			
		}else{
			System.out.println("The ELSE has happeneded");
			data[6] = tfCountryCode.getText();
		}

	}catch(Exception country){
		country.printStackTrace();
		JOptionPane.showMessageDialog(null, "Country must be a proper integer number");
	}
	
	
	//data[7] = tfCountryName.getText();
	//we don't need data seven for saving, just for populating
	
	data[7] = tfPhone.getText();

	data[8] = tfFax.getText();
	
	data[9] = tfNif.getText();
	
	data[10] = tfContact.getText();
	
	data[11] = tfEmail.getText();
	
	//System.out.println("This is the data String" + data[0]+data[1]+data[2]+data[3]+data[4]+data[5]+data[6]+data[7]+data[8]+data[9]+data[10]+data[11]+data[12]);
	
	//System.out.println("this is the string of data from Addressee in the method save" + data[0] + data[1] + data[2]);
		MyAddressee addressee = new MyAddressee(data);
		//System.out.println("this is the data inside de object in the method save" + Tax.getTaxID() + Tax.getTaxName());
		try{
			DatabaseExtension.saveAddressee(addressee);
		}catch (Exception saveAddressee){
			
			saveAddressee.printStackTrace();
			System.out.println("this is the exception released in saving an addressee");
			
			
		}
			resetFields();
			setTextCodeFocus();

	}
	//method that calls the searchwindow
	public void search(){
		//Glob.type = SearchType.TAX;
		//System.out.println("This is the globa variable in Tax when pressing search button" + Glob.type);

		
		
		SearchWindow s = SearchWindow.getInstance();
		s.setTitle();
		s.setVisible(true);
		disableAddressee();
	}
	//method to disable the screen
	public void disableAddressee(){
		Addressee.getInstance().disable();
		disable = true;
	}
	//method to enable the screen
	public void enableAddressee(){
		Addressee.getInstance().enable();
		disable = false;
		if (Glob.type ==  SearchType.ADDRESSEE){
			tfCode.requestFocus();
		}else if (Glob.type ==  SearchType.ADDRESSEECOUNTRY){
			tfCountryCode.requestFocus();
		}
		
		Addressee.getInstance().toFront();
	}
	//method to get the disable boolean
	public boolean getDisable(){
		return disable;
	}
	/**
	//method that calls the dialog screen to remove an item
	public void remove(){
		
		//set up global variable for this screen
		Glob.dialog= DialogType.DELETE;
		//set up global variable for deleting an item
		Glob.type = SearchType.ADDRESSEE;
		System.out.println("This is the global dialog constant inside the remove method " + Glob.dialog);
		try{
			//if the value on the code textfield is empty
			if(Integer.valueOf(tfCode.getText()) <= 0) {
				JOptionPane.showMessageDialog(null, "Code may not be empty and must have a proper ID");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
			}
			//getting all the ids from Tax table in the database
			ArrayList<Integer> ids = DatabaseExtension.columnIDs("Select * from Addressee");
			System.out.println(ids);
			boolean ok = false;
			for (int i: ids) {
				if (i == Integer.valueOf(tfCode.getText()))
						ok=true;
			}
				//if the id is not on the table is not possible to remove it
				if (!ok) {
				JOptionPane.showMessageDialog(null, "This ID is not in the Addressee table");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
			}
			//calling the dialog screen
			Dialog d = Dialog.getInstance();
			d.setVisible(true);
			//disable Tax screen
			disableAddressee();
			}catch(Exception z){
				//code textfield has to contain a proper value and not be empty
				JOptionPane.showMessageDialog(null, "Code may not be empty and must have a proper ID");
				tfCode.requestFocus();
				tfCode.setText(null);
				z.printStackTrace();
			}
	}
	*/
	//method that populates the screen fields with the object
	public void enterData(){
		try{
			//set up global variable for the Tax screen
			Glob.type = SearchType.ADDRESSEE;
			//if textfield is 0 or less than 0
			if(tfCode.getText().trim().length()<1){
				tfName.requestFocus();
				
			}
			else if(Integer.parseInt(tfCode.getText()) <= 0){
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				tfCode.setText("");
				tfCode.requestFocus();
				return;
			}
		//Select the row that will populate the screen
		NewTirDataBase a = (MyAddressee) DatabaseExtension.getTableID("SELECT * FROM addressee WHERE AddresseeID = '" + tfCode.getText() +"'")[0];
		//NewTirDataBase a = (MyAddressee) DatabaseExtension.getTableID("Select AddresseeID, addressee.name , address, postcode, town, province, addressee.countryID, country.name, phonenumber, fax, nif, contact, email from addressee, country where addressee.countryid = country.countryID and addresseeID = '" + tfCode.getText() +"'")[0];
		
		System.out.println(((MyAddressee) a).getAddresseeID());
		//populating the fields
		tfName.setText(((MyAddressee) a).getName());
		tfAddress.setText(((MyAddressee) a).getAddress());
		tfPostcode.setText(((MyAddressee) a).getPostcode());
		tfTown.setText(((MyAddressee) a).getTown());
		tfProvince.setText(((MyAddressee) a).getProvince());
		try{
			//System.out.println("This is the value of countryID in the addressee table " + String.valueOf(((MyAddressee) a).getCodeCountryID()));
			if(String.valueOf(((MyAddressee) a).getCodeCountryID()).equals("null")){
				
				tfCountryCode.setText("");
				
			}else{
			tfCountryCode.setText(((MyAddressee) a).getCodeCountryID());
			}
		
			}catch(Exception e){
				System.out.println("This is the exception regarding the country code");
				e.printStackTrace();
			}
		
		tfCountryName.setText(((MyAddressee) a).getCountryName());
		tfPhone.setText(((MyAddressee) a).getPhoneNumber());
		tfFax.setText(((MyAddressee) a).getFax());
		tfNif.setText(((MyAddressee) a).getNif());
		tfContact.setText(((MyAddressee) a).getContact());
		tfEmail.setText(((MyAddressee) a).getEmail());
		
		
		//System.out.println("this is the Tax id and name" + c.getTaxID() + c.getTaxName());
		System.out.println("intro has been pressed");
		//requestfocus for the next field when pressing enter
		tfName.requestFocus();
		}catch(ArrayIndexOutOfBoundsException exception){
			System.out.println("This is the exception release when id not in database");
			//if there is no id in the database tfName set to empty
			//tfName.setText(null);
			exception.printStackTrace();
			//requestfocus for the next field when pressing enter
			tfName.requestFocus();
		}catch(NumberFormatException exception){
			//if code tf is empty
			/**
			if(tfCode.getText().trim().length()<1){
				System.out.println("This is the exception release when there is an String");
				return;
			}else{
				//set code tf to null
				tfCode.setText(null);
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				tfCode.requestFocus();
			} 
			*/
			JOptionPane.showMessageDialog(null, "Code must be a positive number");
			tfCode.setText(null);
			System.out.println("This is the exception release when there is an String");
			exception.printStackTrace();
		}catch(Exception exception){
			System.out.println("General Exception");
			exception.printStackTrace();
		}
	}
	//method to close the screen
	public void escape(){
		Addressee.getInstance().dispose();
		Menu.getInstance().enableMenu();
	}
	
	/**
	public void enterDataCountry(){
		try{
			//set up global variable for the country screen
			Glob.type = SearchType.COUNTRY;
			//if textfield is 0 or less than 0
			if(Integer.valueOf(tfCountryCode.getText()) <= 0){
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				tfCountryCode.setText("");
				tfCountryCode.requestFocus();
				return;
			}
		//Select the row that will populate the screen
		NewTirDataBase c = (MyCountry) DatabaseExtension.getTableID( "SELECT * FROM Country WHERE CountryID = '" + tfCountryCode.getText() +"'")[0];
		System.out.println(((MyCountry) c).getCountryID());
		//populating the fields
		tfCountryName.setText(((MyCountry) c).getCountryName());
		//System.out.println("this is the country id and name" + c.getCountryID() + c.getCountryName());
		System.out.println("intro has been pressed");
		//requestfocus for the next field when pressing enter
		tfPhone.requestFocus();
		}catch(ArrayIndexOutOfBoundsException exception){
			System.out.println("This is the exception release when id not in database");
			//if there is no id in the database tfName set to empty
			JOptionPane.showMessageDialog(null, "Code is not in the database");
			tfCountryCode.setText(null);
			exception.printStackTrace();
			//requestfocus for the next field when pressing enter
			//tfCountryName.requestFocus();
		}catch(NumberFormatException exception){
			//if code tf is empty
			if(tfCountryCode.getText().trim().length()<1){
				tfPhone.requestFocus();
				return;
			}else{
				//set code tf to null
				tfCountryCode.setText(null);
				JOptionPane.showMessageDialog(null, "Code must be a positive number");
				tfCountryCode.requestFocus();
			} 
			System.out.println("This is the exception release when there is an String");
			exception.printStackTrace();
		}catch(Exception exception){
			System.out.println("General Exception");
			exception.printStackTrace();
		}
	}
	*/
	public boolean checkIdOnTable(JTextField textfield, String query) throws Exception{
		boolean ok = false;
		try{
		ArrayList<Integer> ids = DatabaseExtension.columnIDs(query);
		//System.out.println(ids);
		
		
		if(textfield.getText().trim().length()<1) {
		//Do nothing
		}else{
		
			
		for (int i: ids) {
			if (i == Integer.parseInt(textfield.getText()))
					ok=true;
		}	
		//System.out.println("This is the ok variable" + ok);
			if (ok == false) {
			JOptionPane.showMessageDialog(null, "This ID is not on the Country table");
			textfield.requestFocus();
			textfield.setText("");
			throw new Exception();
			}
			
		}
		}catch(NumberFormatException alpha){
			alpha.printStackTrace();
			JOptionPane.showMessageDialog(null, "The code must be an integer");
			textfield.requestFocus();
			textfield.setText("");
			throw new Exception();
		}
		return ok;
	}
}
